<?php
/*------------------------------------------------------------------------
# Copyright (C) 2004-2008 J.O.O.M Solutions Co., Ltd. All Rights Reserved.
# @license - Copyrighted Commercial Software
# Author: J.O.O.M Solutions Co., Ltd
# Websites:  http://www.joomlart.com -  http://www.joomlancers.com
-------------------------------------------------------------------------*/

jimport( 'joomla.application.component.view');

class JanewsViewJanews extends JView
{
	function display($tpl = null)
	{
		$model = &$this->getModel();
		
		global $mainframe;
		
		$params = $mainframe->getParams();
		$show_fp = $params->get( 'show' );
		$id = $params->get( 'id' );
		
		$view = JRequest::getVar('show');
		if ($view) {
		  $show_fp = 1;
		  $id = (int) JRequest::getVar( 'id' );
		}

		$row = !$show_fp ? $model->getFrontpageInfo() : $model->getSectionInfo($id);

		$headline = false;//disable headline if headline field = 0
    if (!$row) {
      $seclink   = JRoute::_(ContentHelperRoute::getSectionRoute($id));
      $mainframe->redirect(str_replace('&amp;', '&', $seclink));
    }else {
      $this->assignRef( 'section', $model->getSection($id) );
      
  		if ($row->headline > 0)
  		{
  			$headline = true;
  
  			// parameters of headline
  			$hparams = $model->getTemplInfo($row->headline);
  			$file = JPATH_COMPONENT_SITE.DS.'layouts'.DS.'headline'.DS.$hparams->template.DS.'params.xml';
  			$hparams->params = new JParameter($hparams->params,$file);
  			
  			$head_file = JPATH_COMPONENT_SITE.DS.'layouts'.DS.'headline'.DS.$hparams->template.DS.'headline.php';
  			
  			//prepare printf headline
  			$numberofheadlinenews = $hparams->params->get('numberofheadlinenews',3);
  			$strcats = !$show_fp ? null : trim($row->categories);
  			$hlarticles_id = '';
  			
  			switch ($hparams->params->get('listofhlcontent')) {
  			  case 0:
  			    $listofhlcontent = 0;
  			    break;
  			  case 1:
  			    $listofhlcontent = 1;
  			    break;
  			  case 2:
  			    $listofhlcontent = 2;
  			    $hlarticles_id = $hparams->params->get('hlarticles_id');
  			    break;
  			}

  			$hlrows = $model->getHLNews($numberofheadlinenews,$show_fp,$strcats,$listofhlcontent,$hlarticles_id);
  	    $hlids = $model->getHeadlineIds($numberofheadlinenews,$show_fp, $strcats, $listofhlcontent, $hlarticles_id);
  			$this->assignRef( 'hparams',	$hparams );
  			$this->assignRef( 'hlrows',	$hlrows );
  			$this->assignRef( 'head_file', $head_file );
  		}
  		$this->assignRef( 'headline',	$headline );

		  // parameters of blog
  		$bparams = $model->getTemplInfo($row->blog);
		  
  		$file = JPATH_COMPONENT_SITE.DS.'layouts'.DS.'blog'.DS.$bparams->template.DS.'params.xml';
  		$bparams->params = new JParameter($bparams->params,$file);

  		//Prepare printf blog
  		$secid = !$show_fp ? trim($row->sections) : trim($row->categories);
      $secids = split(',',$secid);
      $cols	= 	$bparams->params->get('columns',2);
  		if ($cols > 0) {
  		  $introitems	= 	intval (trim( $bparams->params->get( 'introitems', 1 ) ));
  		  $linkitems	= 	intval (trim( $bparams->params->get( 'linkitems', 0 ) ));
  		  $contents = $themes = array();
  		  
  		  if ($bparams->params->get('datatype')=='no_group_by'){
  		    
  		    $total = $model->getTotalListNoGroup(!$show_fp, $secid, $hlids);
  		    
  		    $limitstart	= JRequest::getVar('limitstart', 0, '', 'int');
  		    $intro = $bparams->params->get('introitems', 4);
      		$links = $bparams->params->get('linkitems', 4);
          $contents = $model->getListNoGroup(!$show_fp, $secid, $hlids, $limitstart,$intro+$links);
      		$limit	= $intro + $links;
      		$user		=& JFactory::getUser();
      		JRequest::setVar('limit', (int) $limit);
  		    jimport('joomla.html.pagination');
		      $pagination = new JPagination($total, $limitstart, $limit - $links);

		      // Create a user access object for the user
      		$access					= new stdClass();
      		$access->canEdit		= $user->authorize('com_content', 'edit', 'content', 'all');
      		$access->canEditOwn		= $user->authorize('com_content', 'edit', 'content', 'own');
      		$access->canPublish		= $user->authorize('com_content', 'publish', 'content', 'all');

      		
		      $this->assign('total',			$total);
		      $this->assignRef('user',		$user);
		      $this->assignRef('pagination',	$pagination);
		      $this->assignRef('access',		$access);
    		}else {
      		$order = $bparams->params->get('order', 'created desc');
          $contents = $model->getBlogContentGroup(!$show_fp, $secids,$linkitems + $introitems,$hlids,$order);
  		  }
  			
  		}
  		
      $blog_file = JPATH_COMPONENT_SITE.DS.'layouts'.DS.'blog'.DS.$bparams->template.DS.'blog.php';
  		
  		$this->assignRef( 'bparams',	$bparams );
  		$this->assignRef( 'secid', $secid );
  		$this->assignRef( 'contents', $contents );
  		$this->assignRef( 'themes', $themes );
  		$this->assignRef( 'show_fp', $show_fp );
  		
  		$this->assignRef( 'blog_file', $blog_file );
		}
		parent::display($tpl);
	}
	
	function &getItem( $index = 0, &$params)
	{
		global $mainframe;

		// Initialize some variables
		$user		=& JFactory::getUser();
		$dispatcher	=& JDispatcher::getInstance();


		$task		= JRequest::getCmd('task');

		$linkOn		= null;
		$linkText	= null;

		$item =& $this->contents[$index];
		$item->text = $item->introtext;

		// Get the page/component configuration and article parameters
		$item->params = clone($params);
		$aparams = new JParameter($item->attribs);

		// Merge article parameters into the page configuration
		$item->params->merge($aparams);

		// Process the content preparation plugins
		JPluginHelper::importPlugin('content');
		$results = $dispatcher->trigger('onPrepareContent', array (& $item, & $item->params, 0));

		// Build the link and text of the readmore button
		if (($item->params->get('show_readmore') && @ $item->readmore) || $item->params->get('link_titles'))
		{
			// checks if the item is a public or registered/special item
			if ($item->access <= $user->get('aid', 0))
			{
				//$item->readmore_link = JRoute::_("index.php?view=article&id=".$item->slug);
				$item->readmore_link = JRoute::_(ContentHelperRoute::getArticleRoute($item->slug, $item->catslug, $item->sectionid));
				$item->readmore_register = false;
			}
			else
			{
				$item->readmore_link = JRoute::_("index.php?option=com_user&task=register");
				$item->readmore_register = true;
			}
		}

		$item->event = new stdClass();
		$results = $dispatcher->trigger('onAfterDisplayTitle', array (& $item, & $item->params,0));
		$item->event->afterDisplayTitle = trim(implode("\n", $results));

		$results = $dispatcher->trigger('onBeforeDisplayContent', array (& $item, & $item->params, 0));
		$item->event->beforeDisplayContent = trim(implode("\n", $results));

		$results = $dispatcher->trigger('onAfterDisplayContent', array (& $item, & $item->params, 0));
		$item->event->afterDisplayContent = trim(implode("\n", $results));

		return $item;
	}
}
?>
